#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define all(x) (x).begin(),(x).end()
typedef long long ll;

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<VI> VVI;

typedef pair<int,int> PII;
typedef vector<PII> VPII;
#define X first
#define Y second
#define mp make_pair

#define two(x) (1<<(x))
#define twoll(x) ((long long)1<<(x))
#define contain(s,x) (((s)&two(x))!=0)
#define containll(s,x) (((s)&twoll(x))!=0)

#define db(a) cout << #a << "=" << a << " "
#define dbn cout << endl
#define sqr(x) (x)*(x)

string STR(int n) { char tmp[20]; sprintf(tmp,"%d",n); return tmp; }
int INT(string s) { return atoi(s.c_str()); }
template <class Ty, class Tx> Ty to(const Tx &x) { Ty y; stringstream ss; ss<<x; ss>>y; return y; }

const int INF = 1000000007;
const double EPS = 1e-10;

char buf[1<<16];
int TC;

int N;
int R[10],PC,mem;
const int MIN = -32768, MAX = 32767;
bool ok;
int ptr,ptrloop;
VS pg;

void check(int val) {
	if (val < MIN || val > MAX) ok = 0;
}

bool endCond() {
	stringstream ss(pg[ptr]);
	string str;
	ss>>str;
	if (str == "dnoc")
		return 1;
	return 0;
}

void skipCond() {
	while (true) {
		++ptr;
		if (endCond()) {
			break;
		}
	}
}

bool endLoop() {
	stringstream ss(pg[ptr]);
	string str;
	ss>>str;
	if (str == "pool")
		return 1;
	return 0;
}

void skipLoop() {
	while (true) {
		++ptr;
		if (endLoop()) {
			break;
		}
	}
}

void proc(string line) {
	stringstream ss(line);
	string cmd; ss >> cmd;
	string reg,val;
	if (cmd == "load") {
		PC++;
		ss >> reg;
		int idx = reg[1]-'0';
		R[idx] = mem;
	} else if (cmd == "store") {
		PC++;
		ss >> val;
		if (val[0] == 'R') {
			int idx = val[1]-'0';
			mem = R[idx];
		} else {
			mem = atoi(val.c_str());
			check(mem);
		}
	} else if (cmd == "move") {
		PC++;
		ss >> reg >> val;
		int idxto = reg[1]-'0';
		if (val[0] == 'R') {
			int idx = val[1]-'0';
			R[idxto] = R[idx];
		} else {
			R[idxto] = atoi(val.c_str());
			check(R[idxto]);
		}
	} else if (cmd == "add") {
		PC++;
		ss >> reg >> val;
		int idxto = reg[1]-'0';
		if (val[0] == 'R') {
			int idx = val[1]-'0';
			R[idxto] += R[idx];
		} else {
			R[idxto] += atoi(val.c_str());
		}
		check(R[idxto]);
	} else if (cmd == "sub") {
		PC++;
		ss >> reg >> val;
		int idxto = reg[1]-'0';
		if (val[0] == 'R') {
			int idx = val[1]-'0';
			R[idxto] -= R[idx];
		} else {
			R[idxto] -= atoi(val.c_str());
		}
		check(R[idxto]);
	} else if (cmd == "loop") {
		PC += 3;
		ptrloop = ptr;
		ss >> reg;
		int idx = reg[1]-'0';
		if (R[idx] <= 0) {
			skipLoop();
		}
	} else if (cmd == "pool") {
		PC += 3;
		ptr = ptrloop-1;
	} else if (cmd == "cond") {
		PC += 3;
		ss >> reg;
		int idx = reg[1]-'0';
		if (R[idx] <= 0) {
			skipCond();
		}
	} else if (cmd == "dnoc") {
		
	}
}

int main() {
#define SUBMIT 0
#if !SUBMIT
	freopen("DATA.in","r",stdin);
#endif

	gets(buf); TC = atoi(buf);
	
	while (TC--) {
		ok = 1;
		pg.clear();
		memset(R,0,sizeof(R)); PC = 0; mem = 0;
		gets(buf); N = atoi(buf);
		
		for (int i = 0; i < N; ++i)
			gets(buf), pg.push_back(buf);
		ptr = 0;
		while (ptr < pg.size()) {
			proc(pg[ptr]);
			ptr++;
			if (!ok) break;
		}
		
		if (ok)
			printf("%d\n",PC+2);
		else
			printf("error\n");
	}

	return 0;
}

























